//
//  MoreViewController.m
//  Quick_News
//
//  Created by 曾凯峻 on 15/1/29.
//  Copyright (c) 2015年 zkj. All rights reserved.
//

#import "MoreViewController.h"

#import "CollectController.h"


#import "AboutWeViewController.h"

@interface MoreViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *tableArr;
@property (nonatomic, retain) NSArray *imageArr;
@property (nonatomic, retain) UILabel *background;


@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"更多";
    
    //  设置导航栏左边返回按钮
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(barButtonAction:)]autorelease];


    self.view.backgroundColor = [UIColor whiteColor];
    self.tableArr = [NSArray arrayWithObjects:@"我的收藏",@"夜间模式",@"清除缓存",@"版本",@"关于我们",@"去App Store评分", nil];
    self.imageArr = [NSArray arrayWithObjects:@"more-shoucang.png",@"more-yejian.png",@"more-qingchu.png",@"more-banben.png",@"more-guangyu.png",@"more-qudafen.png", nil];
    
    self.navigationController.navigationBar.translucent = NO;
    [self creatView];
}

- (void)dealloc
{
    [self.tableView release];
    [self.tableArr release];
    [self.background release];
    [self.imageArr release];
    [super dealloc];
}

- (void)barButtonAction:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatView
{

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.contentSize =  CGSizeMake(0, self.view.frame.size.height + 5);
    [self.view addSubview:scrollView];
    [scrollView release];
    
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
   // imageView.backgroundColor = [UIColor greenColor];
    imageView.image = [UIImage imageNamed:@"moreHanderImage.jpg"];
    [scrollView addSubview:imageView];
    [imageView release];
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width , self.view.frame.size.height - 300) style:UITableViewStylePlain];
  //  self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //设置不可移动
    self.tableView.scrollEnabled = NO;
    [scrollView addSubview:self.tableView];
    [self.tableView release];
    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArr.count;
//    return 6;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }

    NSString *massage = [self.tableArr objectAtIndex:indexPath.row];
    NSString *image = [self.imageArr objectAtIndex:indexPath.row];
    cell.textLabel.text = massage;
    cell.imageView.image = [UIImage imageNamed:image];
    //创建开关
    if (indexPath.row == 1) {
    UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 5, 30, 30)];
         sw.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"kj"];
        [sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        [cell addSubview:sw];
        [sw release];
        
        if (indexPath.row == 3) {
            cell.detailTextLabel.text = @"1.0";
        }
       
    }
    return cell;
}
- (void)switchAction:(id)sender
{
    UISwitch *mySwitch = (UISwitch *)sender;
    //单例
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //将上面是否开启的这个bool值 存到 NSUserdefaults 中
    [defaults setBool:mySwitch.isOn forKey:@"kj"]; 
    [defaults synchronize];
    //发送夜间模式消息
    [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil];
   
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    //收藏
    if (indexPath.row == 0) {

        CollectController *collect = [[CollectController alloc] init];
        [self.navigationController pushViewController:collect animated:YES];
        [collect release];
    }
    //清除缓存
    
    if (indexPath.row == 2) {
        [self clearStore];

    }
    
    
    
    //点击关于我们
    if (indexPath.row == 4) {
        [self aboutWe];
    }
    
}
//关于跳转界面
- (void)aboutWe
{
    AboutWeViewController *aboutVC = [[AboutWeViewController alloc] init];
    [self.navigationController pushViewController:aboutVC animated:YES];
    [aboutVC release];
}
//清理缓存方法
- (void)clearStore
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"清理缓存成功" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    [alertView release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
